import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { logItemsEntity, ColumnItem } from './account-details.entity';
import { AccountDetailsService } from './account-details.service';
import Singleton from '../../services/singleton';

@Component({
  selector: 'app-account-details',
  templateUrl: './account-details.component.html',
  styleUrls: ['./account-details.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AccountDetailsComponent implements OnInit {
  singleton_username: Singleton;
  username: string;
  
  isVisible: boolean = false;
  modalindex: number;
  operator_count: number;

  remaining_num: number = 0;
  get_remaining_num_state: boolean;
  long_remaining_str: string;

  logItems: logItemsEntity[];
  logContainerTab: string[] = ['10%', '10%', '10%'];

  // ajson: string = "{\"transactionHash\":\"0x639be154b7a8f2e5cefcc26e959d17395b18d2872443e50030592559ad9b4a2e\",\"transactionIndex\":0,\"blockHash\":\"0x34b3586c4cb194a4ba94bc63349e6139cefeec2a449b2ff741fb6ac5143bf022\",\"blockNumber\":173,\"gasUsed\":32424,\"contractAddress\":\"0x0000000000000000000000000000000000000000\",\"root\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"status\":\"0x0\",\"message\":\"success\",\"from\":\"0x8fb303dca2ef600037c66acc4a62ad25e180935d\",\"to\":\"0x6afb0665ae61fde96e1e57086d7a546b46e98e2a\",\"input\":\"0xedabf5a6000000000000000000000000763af809531e14c7f7a02e82eb7e067b941c7a85\",\"output\":\"0x0000000000000000000000000000000000000000000000000000000000002700\",\"logs\":[{\"logIndex\":null,\"transactionIndex\":null,\"transactionHash\":null,\"blockHash\":null,\"blockNumber\":null,\"address\":\"0xb636cdc78635d2903e1f58d92cbdc64344cd61ab\",\"data\":\"0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000066875617765690000000000000000000000000000000000000000000000000000\",\"topics\":[\"0x031cbe0b93a7a35a3955c7601f9e8c71d67a16824991e3751b73eaf947281661\"]},{\"logIndex\":null,\"transactionIndex\":null,\"transactionHash\":null,\"blockHash\":null,\"blockNumber\":null,\"address\":\"0xb636cdc78635d2903e1f58d92cbdc64344cd61ab\",\"data\":\"0x0000000000000000000000000000000000000000000000000000000000002700000000000000000000000000763af809531e14c7f7a02e82eb7e067b941c7a85000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000066875617765690000000000000000000000000000000000000000000000000000\",\"topics\":[\"0xe50552dceae8e045627d8d1ced8c9dee2711c1d24f93a32e9181c0ab4309eb50\"]}],\"logsBloom\":\"0x00000000000000000000000000000000000000000000000000000000000000080008000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000010000000000000000000000000000000000000000000000000000000000000000000000008000\",\"txProof\":null,\"receiptProof\":null}";

  
  constructor(
    private readonly accountDetailsService: AccountDetailsService,
    private def:ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    this.singleton_username = Singleton.getInstance();
    this.username = this.singleton_username.getUserName();
    if (this.username == undefined) {
      alert('请右上角退出登录后重新登录');
    }
    this.logItems = this.accountDetailsService.getlogItemsWhole(this.username);
    console.log(this.logItems);

    this.accountDetailsService.getRemainingMoney(this.username).subscribe(str => {
      this.get_remaining_num_state = str.state;
      this.long_remaining_str = str.response;
      let bjson = JSON.parse(this.long_remaining_str);
      this.remaining_num = Number(bjson.output);
      this.def.detectChanges();
    });

    // let ss = JSON.parse(this.ajson);
    // console.log(ss);
  }

  listOfColumns: ColumnItem[] = [
    {
      name: '金额',
      sortOrder: null,
      sortFn: (a: logItemsEntity, b: logItemsEntity) => a.moneyCount - b.moneyCount,
    },
    {
      name: '时间',
      sortOrder: null,
      sortFn: (a: logItemsEntity, b: logItemsEntity) => a.time.localeCompare(b.time),
    }
  ];

  num2str(num: number): string {
    return String(num);
  }

  showModal(num: number) {
    this.modalindex = num;
    this.isVisible = true;
  }

  handleSettingCancel(): void {
    this.isVisible = false;
  }

  handleSettingOk(): void {
    if (this.modalindex === 0) {
      this.accountDetailsService.postIncRemainingMoney(this.username, this.operator_count).subscribe(str => {
        if (str.state === true) {
          this.operator_count = 0;
          this.isVisible = false;
          this.accountDetailsService.getRemainingMoney(this.username).subscribe(str => {
            this.get_remaining_num_state = str.state;
            this.long_remaining_str = str.response;
            let bjson = JSON.parse(this.long_remaining_str);
            this.remaining_num = Number(bjson.output);
            this.def.detectChanges();
          });
        } else {
          alert('wrong');
        }
      });
    }
    if (this.modalindex === 1) {
      this.accountDetailsService.postDecRemainingMoney(this.username, this.operator_count).subscribe(str => {
        if (str.state === true) {
          this.operator_count = 0;
          this.isVisible = false;
          this.accountDetailsService.getRemainingMoney(this.username).subscribe(str => {
            this.get_remaining_num_state = str.state;
            this.long_remaining_str = str.response;
            let bjson = JSON.parse(this.long_remaining_str);
            this.remaining_num = Number(bjson.output);
            this.def.detectChanges();
          });
        } else {
          alert('wrong');
        }
      });
    }
  }
}
